﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.ObjectBuilder;
using Metro.Admin.Views;

public partial class Admin_CitiesCountries : Microsoft.Practices.CompositeWeb.Web.UI.Page, ICitiesCountries
{
    private CitiesCountriesPresenter _presenter;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this._presenter.OnViewInitialized();
        }
        this._presenter.OnViewLoaded();
    }

    [CreateNew]
    public CitiesCountriesPresenter Presenter
    {
        set
        {
            this._presenter = value;
            this._presenter.View = this;
        }
    }

    #region ICitiesCountries Members

    public void ShowCountries(System.Collections.Generic.List<Metro.Admin.BusinessEntities.Country> countries)
    {
        CountryContainerDataSource.DataSource = countries;
        CountriesGridView.Visible = true;

        CountriesContainerDataSource.DataSource = countries;
    }

    public void CreateCountry(Metro.Admin.BusinessEntities.Country country)
    {
        this._presenter.CreateCountry(country); 
    }

    public void UpdateCountry(Metro.Admin.BusinessEntities.Country country)
    {
        this._presenter.UpdateCountry(country);   
    }

    public void DeleteCountry(Metro.Admin.BusinessEntities.Country country)
    {
        this._presenter.DeleteCountry(country); 
    }

    public void UpdateCity(Metro.Admin.BusinessEntities.City city)
    {
        this._presenter.UpdateCity(city); 
    }

    public void CreateCity(Metro.Admin.BusinessEntities.City city, string idCountry)
    {
        this._presenter.CreateCity(city, idCountry); 
    }

    public void DeleteCity(Metro.Admin.BusinessEntities.City city)
    {
        this._presenter.DeleteCity(city);
    }

    public void ShowCities(System.Collections.Generic.ICollection<Metro.Admin.BusinessEntities.City> cities)
    {
        CityContainerDataSource.DataSource = cities;
        GridViewCities.Visible = true; 
    }

    #endregion

    protected void CountryContainerDataSource_Deleted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.DeleteCountry((Metro.Admin.BusinessEntities.Country)e.Instance);
    }
    protected void CountryContainerDataSource_Inserted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.CreateCountry((Metro.Admin.BusinessEntities.Country)e.Instance);
    }
    protected void CountryContainerDataSource_Updated(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.UpdateCountry((Metro.Admin.BusinessEntities.Country)e.Instance);
    }
    protected void CityContainerDataSource_Deleted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.DeleteCity((Metro.Admin.BusinessEntities.City)e.Instance);
    }
    protected void CityContainerDataSource_Inserted(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        Control control = CityDetailsView.FindControl("ddlCountry");
        DropDownList ddlCountry = (DropDownList)control;
        string countryCode = ddlCountry.SelectedValue;

        this.CreateCity((Metro.Admin.BusinessEntities.City)e.Instance, countryCode); 
    }
    protected void CityContainerDataSource_Updated(object sender, Microsoft.Practices.Web.UI.WebControls.ObjectContainerDataSourceStatusEventArgs e)
    {
        this.DeleteCity((Metro.Admin.BusinessEntities.City)e.Instance);
    }
    //public List<Metro.Admin.BusinessEntities.Country> GetCountries()
    //{
    //    List<Country> countries = null;
    //    countries = this._presenter.GetCountries();
    //    return countries;
    //}
}


